package cn.uc.ucgc.community.upgrade.conf.task;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.dom4j.DocumentException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.uc.ucgc.community.upgrade.Constants;
import cn.uc.ucgc.community.upgrade.Task;
import cn.uc.ucgc.community.upgrade.Utils;
import cn.uc.ucgc.community.upgrade.conf.AddJob;
import cn.uc.ucgc.community.upgrade.conf.JobType;
import cn.uc.ucgc.community.upgrade.conf.MacroItem;

/**
 * 需要添加配置的任务
 * 
 * @author laizy@ucweb.com
 * @createDate 2014-4-1
 *
 */
public class AddTask extends Task {
    private static final Logger logger = LoggerFactory.getLogger(AddTask.class);
    //任务信息
    private List<AddJob> jobs;
    private File macroFile;

    public AddTask(List<AddJob> jobs, File macroFile) {
        super.setTaskDesc("Add File Task");
        this.jobs = jobs;
        this.macroFile = macroFile;
    }
    
    public List<AddJob> getJobs() {
        return jobs;
    }

    @Override
    public void doTask(StringBuilder sb) throws IOException, DocumentException {
        if (null == jobs || jobs.isEmpty()) {
            sb.append(Constants.TASK_RESULT_NOTASK_SUFFIX);
            return;
        }
        //首先将prop任务分开
        List<AddJob> propJobs = new ArrayList<AddJob>();
        for (AddJob job : jobs) {
            if (null == job) {
                continue;
            }
            if (JobType.PROP == job.getType()) {
                propJobs.add(job);
            }
        }
        //读取对应的宏配置文件
        Properties macroProp = Utils.getPropValues(macroFile);
        logger.warn("Need add prop file count:{}", propJobs.size());
        //对prop类型的做处理
        doPropJobs(propJobs, macroProp);
    }

    private void doPropJobs(List<AddJob> propJobs, Properties macroProp) throws IOException {
        //读取prop配置文件，并将对应需要新增的数据更新，然后保存文件
        for (AddJob job : propJobs) {
            //1)获取对应待操作的文件
            File filePath = job.getSrcPath();
            logger.info("prop add:{}", filePath.getCanonicalPath());
            List<MacroItem> items = job.getAddItems();
            if (null == items || items.isEmpty()) {
                logger.warn("no add items");
                continue;
            }

            Map<String, String> addProps = new HashMap<String, String>();
            for (MacroItem item : items) {
                //该配置文件，需要修改的项
                String key = item.getName();
                //首先尝试从宏配置文件中读取
                String macroKey = item.getMacroKey();
                String realVal = macroProp.getProperty(macroKey);
                if (null == realVal) {
                    logger.warn("no macroVal,the macroKey:{}", macroKey);
                    //如果没有的话，则直接丢弃掉该配置项
                    continue;
                }
                addProps.put(key, realVal);
            }
            //根据收集的数据，重写目标文件
            Utils.addConf2PropFile(filePath, addProps);
        }
    }

}